﻿
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Xml
Imports System.IO

Public Class ViewDataTrans


    Public Shared Sub Main()
        Dim tran = New ViewDataTrans()
        tran.DataTrans("C:\\ViewDataTrans\\B0133160.xml", "C:\\ViewDataTrans\\YS1009.out", "11111")
    End Sub


    ''' <summary>
    ''' 观测数据转换,文本路线
    ''' </summary>
    ''' <param name="xmlFileName">原始数据名称名称</param>
    ''' <param name="textFileFold">转换后的数据文件存放路径</param>
    ''' <remarks></remarks>

    Public Function DataTrans(ByVal xmlFileName As String, ByVal textFileFold As String) As Boolean

        Dim file As FileInfo = New FileInfo(textFileFold)
        If (file.Exists) Then

            '给出的路径名称是文件名
            Throw New Exception("不能生成路径文件！" + textFileFold)
        End If

        Dim di As DirectoryInfo = New DirectoryInfo(textFileFold)
        If (di.Exists) Then

        Else
            '文件目录不存在，创建目录
            di.Create()
        End If

        '处理非标准的XML文件为标准文件
        preDealTextFile(xmlFileName)



        Dim xx As XmlDocument = New XmlDocument()
        xx.Load(xmlFileName) ''加载xml
        Dim xxList As XmlNodeList = xx.GetElementsByTagName("水准路线")  '取得节点名为row的XmlNode集合
        For Each xxNode As XmlNode In xxList

            Dim tmpClass As 水准路线 = 水准路线.Paras(xxNode)

            tmpClass.markRouteNO()


            '生成
            '⑴测段信息文件.txt
            TransMeasureSegmentInfoFile(textFileFold + "\\测段信息文件.txt", tmpClass)
            '⑵观测时间文件.txt
            TransViewDateFile(textFileFold + "\\观测时间文件.txt", tmpClass)
            '⑶观测数据文件.txt
            TransViewDataFile(textFileFold + "\\观测数据文件.txt", tmpClass)
            '⑷经纬度原始文件.txt
            TransPositionOriginFile(textFileFold + "\\经纬度原始文件.txt", tmpClass)
            '⑸上下标志观测文件.txt
            TransUDFlagFile(textFileFold + "\\上下标志观测文件.txt", tmpClass)
            '⑹检测测段数据文件.txt
            TranMeasureSegmentDataFile(textFileFold + "\\检测测段数据文件.txt", tmpClass)
            '(7)检测测段数据文件.txt
            TranPositionRouterDataFile(textFileFold + "\\经纬度路线文件.txt", tmpClass)
        Next


        Return True


    End Function
    ''' <summary>
    ''' 观测数据转换‘单文件
    ''' </summary>
    ''' <param name="xmlFileName">原始数据名称名称</param>
    ''' <param name="textFileFold">转换后的数据文件存放路径</param>
    ''' <param name="lineNo">线路编号</param>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Public Function DataTrans(ByVal xmlFileName As String, ByVal textFileFold As String, ByVal lineNo As String) As Boolean

        Dim file As FileInfo = New FileInfo(textFileFold)
        If (file.Exists) Then

            '给出的路径名称是文件名
            Throw New Exception("不能生成路径文件！" + textFileFold)
        End If

        Dim di As DirectoryInfo = New DirectoryInfo(textFileFold)
        If (di.Exists) Then

        Else
            '文件目录不存在，创建目录
            di.Create()
        End If



        Dim xx As XmlDocument = New XmlDocument()
        xx.Load(xmlFileName) ''加载xml
        Dim xxList As XmlNodeList = xx.GetElementsByTagName("测段")  '取得节点名为row的XmlNode集合

        Dim tmpClass As 水准路线 = New 水准路线()
        ' tmpClass.路线名称 = lineNo '路线去掉後3位

        For Each xxNode As XmlNode In xxList
            Dim tmp测段 As 测段 = 测段.Paras(xxNode)
            tmp测段.水准路线 = tmpClass
            tmpClass.测段s.Add(tmp测段)
        Next


        tmpClass.markRouteNO1(lineNo) '在加测段号的地方判断是否为单文件，


        '生成
        '⑴测段信息文件.txt
        TransMeasureSegmentInfoFile(textFileFold + "\\测段信息文件.txt", tmpClass)
        '⑵观测时间文件.txt
        TransViewDateFile(textFileFold + "\\观测时间文件.txt", tmpClass)
        '⑶观测数据文件.txt
        TransViewDataFile(textFileFold + "\\观测数据文件.txt", tmpClass)
        '⑷经纬度原始文件.txt
        TransPositionOriginFile(textFileFold + "\\经纬度原始文件.txt", tmpClass)
        '⑸上下标志观测文件.txt
        TransUDFlagFile(textFileFold + "\\上下标志观测文件.txt", tmpClass)
        '⑹检测测段数据文件.txt
        TranMeasureSegmentDataFile(textFileFold + "\\检测测段数据文件.txt", tmpClass)
        '(7)检测测段数据文件.txt
        TranPositionRouterDataFile(textFileFold + "\\经纬度路线文件.txt", tmpClass)

        Return True

    End Function

    ''' <summary>
    ''' ⑴测段信息文件.txt
    ''' </summary>
    ''' <param name="p"></param>
    ''' <param name="tmpClass"></param>
    ''' <remarks></remarks>
    Private Sub TransMeasureSegmentInfoFile(p As String, tmpClass As 水准路线)
        '"测段信息文件.txt"
        'CDINFO1=  routeNO; markGB; StartName; CD_TQ1; CD_CX1; CD_Ty1; CD_FX1; CD_FS1; CD_DL1; CD_tz1; Right(Startdate1, 6); EndName; CD_TQ2; CD_CX2; CD_Ty2; CD_FX2; CD_FS2; CD_DL2; CD_tz2; Right(Enddate, 6); BTH;开始温度;结束温度
        '格式说明:
        'routeNO=环线号,同上
        'markGB=测量_标志,同上
        'StartName_开始点名,同上;下面为开始段天气
        'CD_TQ1=天气和云量    天气="0晴"和 云量="1云量0级" ;2位数字如"01",只要数字,下同
        '        CD_CX1 = 呈像 = "0清晰、稳定 "
        '        CD_Ty1 = 太阳方向 = "1前右"
        '        CD_FX1 = 风向 = "1东北"
        '        CD_FS1 = 风速 = "1轻风"
        '        CD_DL1 = 道路 = "0柏油路"
        '        CD_tz1 = 土质 = "0柏油"
        '        开始时间, 同上
        '        EndName_结束点名, 同上, 下面为结束段天气
        'CD_TQ2=天气和云量    天气="0晴"和 云量="1云量0级" ;2位数字如"01",只要数字,下同
        '        CD_CX2 = 呈像 = "0清晰、稳定 "
        '        CD_Ty2 = 太阳方向 = "1前右"
        '        CD_FX2 = 风向 = "1东北"
        '        CD_FS2 = 风速 = "1轻风"
        '        CD_DL2 = 道路 = "0柏油路"
        '        CD_tz2 = 土质 = "0柏油"
        '        结束时间, 同上
        'BTH=观测事件数量,同上
        '开始温度= 从<观测事件 标记="GO0" 时间="201208031454" 测站="0" 呈像="0清晰、稳定 " 温度="+33.0"获取
        '结束温度= 从<观测事件 标记="EN " 时间="201208031746" 测站="56" 呈像="0清晰、稳定 " 温度="+31.0" 


        Dim fi As FileInfo = New FileInfo(p)
        Dim fos As FileStream = fi.Open(FileMode.Append, FileAccess.Write)
        Dim sw As StreamWriter = New StreamWriter(fos)
        For Each tmp As 测段 In tmpClass.测段s
            Dim STARNAME, ENDNAME As String
            ENDNAME = "" : STARNAME = ""
            ENDNAME = tmp.getEndName()
            ENDNAME = ENDNAME.Replace("（", "(")   '2012-10-22
            ENDNAME = ENDNAME.Replace("）", ")")   '2012-10-22
            STARNAME = tmp.getStartName()
            STARNAME = STARNAME.Replace("（", "(")   '2012-10-22
            STARNAME = STARNAME.Replace("）", ")")   '2012-10-22

            Dim strLine As String = tmp.routeNO + "," + tmp.标志 + "," _
                  + tmp.起测点 + "," _
                  + tmp.getCD_TQ1() + "," + tmp.getCD_CX1() + "," + tmp.getCD_TY1() + "," + tmp.getCD_FX1() + "," _
                  + tmp.getCD_FS1() + "," + tmp.getCD_DL1() + "," + tmp.getCD_TZ1() + "," + STARNAME + "," _
                  + ENDNAME + "," _
                  + tmp.getCD_TQ2() + "," + tmp.getCD_CX2() + "," + tmp.getCD_TY2() + "," + tmp.getCD_FX2() + "," _
                  + tmp.getCD_FS2() + "," + tmp.getCD_DL2() + "," + tmp.getCD_TZ2() + "," + tmp.GetEndDate() + "," _
                  + tmp.getBTH() + "," + tmp.getStartTemperature() + "," + tmp.getEndTemperature()

            sw.WriteLine(strLine)
            sw.Flush()
        Next
        sw.Close()
        fos.Close()




    End Sub
    ''' <summary>
    ''' ⑵观测时间文件.txt
    ''' </summary>
    ''' <param name="p"></param>
    ''' <param name="tmpClass"></param>
    ''' <remarks></remarks>
    Private Sub TransViewDateFile(p As String, tmpClass As 水准路线)
        '        "观测时间文件.txt"
        'GCTIME1= apparatusNO; Startdate; Enddate; routeNO; markGB; StartName; EndName
        '格式说明:
        'apparatusNO=标尺号号,看测量标识是G往测还是B返测,与前获取方式一致.
        'Startdate=开始时间,从<观测事件 标记="GO0" 时间="201208031454"获取 
        'Enddate=结束时间,  从<观测事件 标记="EN " 时间="201208031746"获取
        '        routeNO = 环号获取方式同上
        'markGB=测量_标志 获取方式同上获取方式同上,从<测段 标志="GO0" 获取;有"GO0"\"BK0"\"RG1"\"RB1"\"CGO"\"CBK"等
        'StartName = 起测点 从 <测段 标志="GO0" 起测点="山阴(1189)下" 获取起测点名
        'EndName= 结束点    从<测段小结 结束点="Ⅰ大榆25-2基上" 获取结束点名

        Dim fi As FileInfo = New FileInfo(p)
        Dim fos As FileStream = fi.Open(FileMode.Append, FileAccess.Write)
        Dim sw As StreamWriter = New StreamWriter(fos)
        For Each tmp As 测段 In tmpClass.测段s
            Dim STARNAME, ENDNAME As String
            ENDNAME = "" : STARNAME = ""
            ENDNAME = tmp.getEndName()
            ENDNAME = ENDNAME.Replace("（", "(")   '2012-10-22
            ENDNAME = ENDNAME.Replace("）", ")")   '2012-10-22
            STARNAME = tmp.getStartName()
            STARNAME = STARNAME.Replace("（", "(")   '2012-10-22
            STARNAME = STARNAME.Replace("）", ")")   '2012-10-22
            Dim strLine As String
            If tmp.标志 = "GO0" Or tmp.标志 = "RG1" Then
                strLine = tmp.getApparatusNO + "," + tmp.getStartDate + "," + tmp.GetEndDate + "," _
                                        + tmp.routeNO + "," + tmp.标志 + "," + STARNAME + "," + ENDNAME + "," + tmp.getStaffNOG()

            Else
                strLine = tmp.getApparatusNO + "," + tmp.getStartDate + "," + tmp.GetEndDate + "," _
                                        + tmp.routeNO + "," + tmp.标志 + "," + STARNAME + "," + ENDNAME + "," + tmp.getStaffNOB()
            End If


            sw.WriteLine(strLine)
            sw.Flush()
        Next
        sw.Close()
        fos.Close()


    End Sub

    ''' <summary>
    ''' ⑶观测数据文件.txt
    ''' </summary>
    ''' <param name="p"></param>
    ''' <param name="tmpClass"></param>
    ''' <remarks></remarks>
    Private Sub TransViewDataFile(p As String, tmpClass As 水准路线)

        Dim fi As FileInfo = New FileInfo(p)
        Dim fos As FileStream = fi.Open(FileMode.Append, FileAccess.Write)
        Dim sw As StreamWriter = New StreamWriter(fos)
        For Each tmp As 测段 In tmpClass.测段s
            Dim STARNAME, ENDNAME As String
            ENDNAME = "" : STARNAME = ""
            ENDNAME = tmp.getEndName()
            ENDNAME = ENDNAME.Replace("（", "(")   '2012-10-22
            ENDNAME = ENDNAME.Replace("）", ")")   '2012-10-22
            STARNAME = tmp.getStartName()
            STARNAME = STARNAME.Replace("（", "(")   '2012-10-22
            STARNAME = STARNAME.Replace("）", ")")   '2012-10-22
            Dim strLine As String

            If Val(tmp.getDistanceG()) < 0.1 Then
                strLine = tmp.routeNO + "," + tmp.标志 + "," _
                        + tmp.getAM_ZZS() + "," + tmp.getPM_ZZS() + "," + tmp.getZZS() + "," _
                        + tmp.getDJ() + "," + STARNAME + "," + ENDNAME + "," _
                        + tmp.getApparatusNO() + "," + tmp.getStaffNOG() + "," + tmp.getElevationG() + "," _
                        + "0.1" + "," + tmp.getStaffNOB() + "," + tmp.getElevationB() + "," _
                        + tmp.getDistanceB()

            ElseIf Val(tmp.getDistanceB()) < 0.1 Then
                strLine = tmp.routeNO + "," + tmp.标志 + "," _
                          + tmp.getAM_ZZS() + "," + tmp.getPM_ZZS() + "," + tmp.getZZS() + "," _
                          + tmp.getDJ() + "," + STARNAME + "," + ENDNAME + "," _
                          + tmp.getApparatusNO() + "," + tmp.getStaffNOG() + "," + tmp.getElevationG() + "," _
                          + tmp.getDistanceG() + "," + tmp.getStaffNOB() + "," + tmp.getElevationB() + "," _
                          + "0.1"

            Else
                strLine = tmp.routeNO + "," + tmp.标志 + "," _
                   + tmp.getAM_ZZS() + "," + tmp.getPM_ZZS() + "," + tmp.getZZS() + "," _
                   + tmp.getDJ() + "," + STARNAME + "," + ENDNAME + "," _
                   + tmp.getApparatusNO() + "," + tmp.getStaffNOG() + "," + tmp.getElevationG() + "," _
                   + tmp.getDistanceG() + "," + tmp.getStaffNOB() + "," + tmp.getElevationB() + "," _
                   + tmp.getDistanceB()
            End If

            sw.WriteLine(strLine)
            sw.Flush()
        Next
        sw.Close()
        fos.Close()




    End Sub
    ''' <summary>
    ''' ⑷经纬度原始文件.txt
    ''' </summary>
    ''' <param name="p"></param>
    ''' <param name="tmpClass"></param>
    ''' <remarks></remarks>

    Private Sub TransPositionOriginFile(p As String, tmpClass As 水准路线)
        '        "经纬度原始文件.txt"    
        '        jwdfilestring = EndName & "," & routeNO & "," & markGB & "," & Longitude & "," & Latitude & "," & DJ & vbCrLf
        '格式说明:
        'EndName=点名,包括开始点名与结束点名2种,要2个数据行
        '        routeNO = 环线号
        'markGB=测量_标志,同上
        'Longitude=经度  <测段 标志="GO0" 起测点="山阴(1189)下" 前尺号码="34372" 后尺号码="34366" 数据加密方法="ccyyencypt4" 加密校验="12004" 记录程序="ccyylevel09 2.21GB1_D_ARMGPS" 记录器编号="413500001079" 记录器型号="A350" 起测点经度="112.48361"获取
        'Latitude=纬度   <测段 标志="GO0" 起测点="山阴(1189)下" 前尺号码="34372" 后尺号码="34366" 数据加密方法="ccyyencypt4" 加密校验="12004" 记录程序="ccyylevel09 2.21GB1_D_ARMGPS" 记录器编号="413500001079" 记录器型号="A350" 起测点经度="112.48361" 起测点纬度="39.32369"获取
        '        DJ = 等级同上
        Dim fi As FileInfo = New FileInfo(p)
        Dim fos As FileStream = fi.Open(FileMode.Append, FileAccess.Write)
        Dim sw As StreamWriter = New StreamWriter(fos)
        For Each tmp As 测段 In tmpClass.测段s
            Dim STARNAME, ENDNAME As String
            ENDNAME = "" : STARNAME = ""
            ENDNAME = tmp.getEndName()
            ENDNAME = ENDNAME.Replace("（", "(")   '2012-10-22
            ENDNAME = ENDNAME.Replace("）", ")")   '2012-10-22
            STARNAME = tmp.getStartName()
            STARNAME = STARNAME.Replace("（", "(")   '2012-10-22
            STARNAME = STARNAME.Replace("）", ")")   '2012-10-22

            Dim strLine As String = STARNAME + "," + tmp.getRouteNo + "," + tmp.标志 + "," _
                  + tmp.起测点经度 + "," + tmp.起测点纬度 + "," + tmp.getDJ()
            sw.WriteLine(strLine)
            sw.Flush()

            strLine = ENDNAME + "," + tmp.getRouteNo + "," + tmp.标志 + "," _
                  + tmp.Get结束点经度 + "," + tmp.结束点纬度 + "," + tmp.getDJ()
            sw.WriteLine(strLine)
            sw.Flush()

        Next
        sw.Close()
        fos.Close()
    End Sub
    ''' <summary>
    ''' ⑸上下标志观测文件.txt
    ''' </summary>
    ''' <param name="p"></param>
    ''' <param name="tmpClass"></param>
    ''' <remarks></remarks>
    Private Sub TransUDFlagFile(p As String, tmpClass As 水准路线)

        '        上下标志观测文件.txt()
        '        udfile1 = EndName & "," & routeNO & "," & apparatusNO & "," & staffNO & "," & ud_date & "," & UD_Elevation & vbCrLf

        'EndName=点名 从 <数据 点名="山阴(1189)下"获取
        '        routeNO = 环号获取方式同上
        'apparatusNO=仪器号,看测量标识是G往测还是B返测,与前获取方式一致.
        'staffNO=标尺号号,看测量标识是G往测还是B返测,与前获取方式一致.
        'ud_date=观测时间 从<观测事件 标记="UD " 时间="201208031505"获取 
        'UD_Elevation上下标志高差 从<数据 点名="山阴(1189)下" 自="上标志" 至="下标志" 高差="0.98998m"获取   

        Dim fi As FileInfo = New FileInfo(p)
        Dim fos As FileStream = fi.Open(FileMode.Append, FileAccess.Write)
        Dim sw As StreamWriter = New StreamWriter(fos)

        For Each tmp As 测段 In tmpClass.测段s
            For Each tmp1 As 观测事件 In tmp.观测事件s
                Dim STARNAMEUD As String
                STARNAMEUD = ""
                STARNAMEUD = tmp1.getNameUD()
                STARNAMEUD = STARNAMEUD.Replace("（", "(")   '2012-10-22
                STARNAMEUD = STARNAMEUD.Replace("）", ")")   '2012-10-22





                If tmp1.标记 = "UD " Then
                    Dim strLine As String = STARNAMEUD + "," + tmp.getRouteNo + "," + tmp.getApparatusNO + "," _
                          + tmp.getStaffNOB + "," + tmp1.时间 + "," + tmp1.gteElevationUD
                    sw.WriteLine(strLine)
                    sw.Flush()
                End If
            Next
        Next

        sw.Close()
        fos.Close()
    End Sub
    ''' <summary>
    ''' ⑹检测测段数据文件.txt
    ''' </summary>
    ''' <param name="p"></param>
    ''' <param name="tmpClass"></param>
    ''' <remarks></remarks>
    Private Sub TranMeasureSegmentDataFile(p As String, tmpClass As 水准路线)

        '"检测测段数据文件.txt" 
        'GCSJUFILE1=  routeNO; markGB; SN; BTH; ZZS; DJ; StartName; EndName; apparatusNO; staffNOG; ElevationG; distanceG; staffNOB; ElevationB; distanceB
        'JCSJFILE1 = routeNO & "," & markGB & "," & BTH & "," & ZZS & "," & DJ & "," & StartName & "," & EndName & "," & apparatusNO & "," & staffNOG & "," & ElevationG & "," & distanceG & "," & staffNOB & "," & ElevationB & "," & distanceB & vbCrLf
        '数据格式与"观测数据文件.txt"一致,只是当  markGB= "CGO"\"CBK"等时另存成此文件.

        Dim fi As FileInfo = New FileInfo(p)
        Dim fos As FileStream = fi.Open(FileMode.Append, FileAccess.Write)
        Dim sw As StreamWriter = New StreamWriter(fos)
        For Each tmp As 测段 In tmpClass.测段s
            Dim STARNAME, ENDNAME As String
            ENDNAME = "" : STARNAME = ""
            ENDNAME = tmp.getEndName()
            ENDNAME = ENDNAME.Replace("（", "(")   '2012-10-22
            ENDNAME = ENDNAME.Replace("）", ")")   '2012-10-22
            STARNAME = tmp.getStartName()
            STARNAME = STARNAME.Replace("（", "(")   '2012-10-22
            STARNAME = STARNAME.Replace("）", ")")   '2012-10-22



            If tmp.标志 = "CGO" Or tmp.标志 = "CBK" Then
                Dim strLine As String = tmp.routeNO + "," + tmp.标志 + "," _
                      + tmp.getAM_ZZS() + "," + tmp.getPM_ZZS() + "," + tmp.getZZS() + "," _
                      + tmp.getDJ() + "," + STARNAME + "," + ENDNAME + "," _
                      + tmp.getApparatusNO() + "," + tmp.getStaffNOG() + "," + tmp.getElevationG() + "," _
                      + tmp.getDistanceG() + "," + tmp.getStaffNOB() + "," + tmp.getElevationB() + "," _
                      + tmp.getDistanceB()

                sw.WriteLine(strLine)
                sw.Flush()
            End If
        Next
        sw.Close()
        fos.Close()
    End Sub
    ''' <summary>
    ''' (7)检测测段数据文件.txt
    ''' </summary>
    ''' <param name="p"></param>
    ''' <param name="tmpClass"></param>
    ''' <remarks></remarks>
    Private Sub TranPositionRouterDataFile(p As String, tmpClass As 水准路线)

        'jwdLXfilestring = routeNOBH & "," & Longitude & "," & Latitude
        'routeNOBH为routeNO和BH的合体;只记录"GO0"的数据,其他数据不记录(要记录CGO或CBK的数据,)
        'routeNO=路线号,获取方式同上
        'BH=测站编号,从<测站 编号="1"获取,不足3位要补齐3位,如"001"\"010"\"100"等

        'CZ_Longitude=测站经度  从<测站 编号="1" 经度="112.483628" 获取
        'CZ_Latitude=测站纬度   从<测站 编号="1" 经度="112.483628" 纬度="39.323745"获取
        '        如果测站经纬度有一为0, 则测站经纬度数据不保留, 测站空过
        Dim fi As FileInfo = New FileInfo(p)
        Dim fos As FileStream = fi.Open(FileMode.Append, FileAccess.Write)
        Dim sw As StreamWriter = New StreamWriter(fos)

        For Each tmp As 测段 In tmpClass.测段s
            If tmp.标志 = "GO0" Or tmp.标志 = "CGO" Or tmp.标志 = "CBK" Then
                For Each tmp1 As 测站 In tmp.测站s
                    If Val(tmp1.经度) <> 0 And Val(tmp1.经度) <> 0 Then
                        Dim strLine As String = tmp.routeNO & String.Format("{0:000}", Val(tmp1.编号)) + "," _
                                  + tmp1.经度 + "," + tmp1.纬度
                        sw.WriteLine(strLine)
                        sw.Flush()
                    End If
                Next
            End If
        Next

        sw.Close()
        fos.Close()
    End Sub




    ''' <summary>
    ''' 观测数据转换
    ''' </summary>
    ''' <param name="xmlContext">原始XML数据内容</param>
    ''' <param name="textFileFold">转换后的数据文件存放路径</param>
    ''' <remarks></remarks>
    Public Sub DataTransXML(xmlContext As String, textFileFold As String)

    End Sub

    Private Sub preDealTextFile(xmlFileName As String)
        Dim f As FileInfo = New FileInfo(xmlFileName)
        Dim temfile As FileInfo = New FileInfo(Guid.NewGuid().ToString())
        '   temfile.Create()

        Dim sr As StreamReader = New StreamReader(f.OpenRead(), System.Text.Encoding.GetEncoding("gb2312"))
        Dim sw As StreamWriter = New StreamWriter(temfile.Name, False, System.Text.Encoding.GetEncoding("gb2312"))
        Dim text As String
        Dim newAddStopProject As Boolean = False
        Dim newAddStop测段 As Boolean = True
        While (Not sr.EndOfStream)
            text = sr.ReadLine
            If text.Contains("<项目") Then
                '项目开始
                newAddStopProject = True
            End If
            If text.Contains("</项目>") Then
                '观察事件开始
                newAddStopProject = False

            End If

            If text.Contains("</测段>") Then
                '观察事件开始
                newAddStop测段 = False
            End If

            If text.Contains("<观测事件") Then
                '观察事件开始
                If newAddStopProject Then
                    '需要添加项目的结束符号
                    sw.WriteLine("</项目>")
                    newAddStopProject = False
                End If
            End If



            sw.WriteLine(text)

        End While

        If newAddStop测段 Then
            '需要添加项目的结束符号
            sw.WriteLine("</测段>")
        End If

        sw.Close()
        sr.Close()
        f.MoveTo(xmlFileName + "_back" + Guid.NewGuid().ToString)
        temfile.MoveTo(xmlFileName)

    End Sub

End Class
